<html>
<head><meta charset="utf-8"><title>Reason a fn can&#x27;t be const lint? · t-compiler/wg-diagnostics · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/index.html">t-compiler/wg-diagnostics</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Reason.20a.20fn.20can&#x27;t.20be.20const.20lint.3F.html">Reason a fn can&#x27;t be const lint?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="215711292"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Reason%20a%20fn%20can%27t%20be%20const%20lint%3F/near/215711292" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Restioson <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Reason.20a.20fn.20can&#x27;t.20be.20const.20lint.3F.html#215711292">(Nov 05 2020 at 13:33)</a>:</h4>
<p>I was reading <a href="https://blog.darklang.com/first-thoughts-on-rust-vs-ocaml/">this article</a> when I came across this section:</p>
<blockquote>
<p>Programming in Rust reminds me a lot of programming in C++: you add a const to one function, and then you have to follow that const around the entire codebase until you finally get to the place where you learn it actually can't be const, and so fuck you.</p>
</blockquote>
<p>This was originally an analogy for something else in rust but it made me think... would it be possible to make some kind of clippy lint that would run on <code>const</code>-marked functions that don't compile as const which would kind of do the "follwoing the const around" step for you? It would stop when it reached the first non-const function called from another crate. I have no idea how feasible this is at all or what would be required, so it's just a thought. What do people think?</p>



<a name="215711512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Reason%20a%20fn%20can%27t%20be%20const%20lint%3F/near/215711512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Restioson <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Reason.20a.20fn.20can&#x27;t.20be.20const.20lint.3F.html#215711512">(Nov 05 2020 at 13:35)</a>:</h4>
<p>Someone on the rust community discord said this of it:</p>
<blockquote>
<p>the process of running it seems simple enough, go through each function call in the function body, trace the function to its body, and recursively do the same thing again, if the function is 100% not const-able mark it as wrong and exit early with an error.</p>
</blockquote>



<a name="215725333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Reason%20a%20fn%20can%27t%20be%20const%20lint%3F/near/215725333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Reason.20a.20fn.20can&#x27;t.20be.20const.20lint.3F.html#215725333">(Nov 05 2020 at 15:16)</a>:</h4>
<p>maybe something for <a class="stream" data-stream-id="146212" href="/#narrow/stream/146212-t-compiler.2Fconst-eval">#t-compiler/const-eval</a></p>



<a name="215725655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Reason%20a%20fn%20can%27t%20be%20const%20lint%3F/near/215725655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Reason.20a.20fn.20can&#x27;t.20be.20const.20lint.3F.html#215725655">(Nov 05 2020 at 15:19)</a>:</h4>
<p>we do have a lint that suggests you to make functions const fn that could be const fn</p>



<a name="215725670"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Reason%20a%20fn%20can%27t%20be%20const%20lint%3F/near/215725670" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Reason.20a.20fn.20can&#x27;t.20be.20const.20lint.3F.html#215725670">(Nov 05 2020 at 15:19)</a>:</h4>
<p>(in clippy that is)</p>



<a name="215725729"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Reason%20a%20fn%20can%27t%20be%20const%20lint%3F/near/215725729" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Reason.20a.20fn.20can&#x27;t.20be.20const.20lint.3F.html#215725729">(Nov 05 2020 at 15:19)</a>:</h4>
<p>it's false positive free I think, but has lots of false negatives (so it may not tell you about a function, even i fthat funciton could be const fn)</p>



<a name="215726826"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Reason%20a%20fn%20can%27t%20be%20const%20lint%3F/near/215726826" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Reason.20a.20fn.20can&#x27;t.20be.20const.20lint.3F.html#215726826">(Nov 05 2020 at 15:26)</a>:</h4>
<p>oh... I misunderstood the original post</p>



<a name="215726864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Reason%20a%20fn%20can%27t%20be%20const%20lint%3F/near/215726864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Reason.20a.20fn.20can&#x27;t.20be.20const.20lint.3F.html#215726864">(Nov 05 2020 at 15:26)</a>:</h4>
<p>ok, this is more <a class="stream" data-stream-id="147480" href="/#narrow/stream/147480-t-compiler.2Fwg-diagnostics">#t-compiler/wg-diagnostics</a> so I'm moving it there</p>



<a name="215726904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Reason%20a%20fn%20can%27t%20be%20const%20lint%3F/near/215726904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Notification Bot <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Reason.20a.20fn.20can&#x27;t.20be.20const.20lint.3F.html#215726904">(Nov 05 2020 at 15:27)</a>:</h4>
<p>This topic was moved here from <a class="stream-topic" data-stream-id="257328" href="/#narrow/stream/257328-clippy/topic/Reason.20a.20fn.20can.27t.20be.20const.20lint.3F">#clippy &gt; Reason a fn can't be const lint?</a> by <span class="user-mention silent" data-user-id="124288">oli</span></p>



<a name="215727083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Reason%20a%20fn%20can%27t%20be%20const%20lint%3F/near/215727083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Reason.20a.20fn.20can&#x27;t.20be.20const.20lint.3F.html#215727083">(Nov 05 2020 at 15:28)</a>:</h4>
<p>So... basically, when we report an error in a const fn because you called a non-const-fn, we may want to figure out why that function is not const fn other than not having a <code>const</code> attached to it</p>



<a name="215727202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Reason%20a%20fn%20can%27t%20be%20const%20lint%3F/near/215727202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Reason.20a.20fn.20can&#x27;t.20be.20const.20lint.3F.html#215727202">(Nov 05 2020 at 15:29)</a>:</h4>
<p>and if there are functions in the call graph that can't be const fn, we tell you about that</p>



<a name="215751047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Reason%20a%20fn%20can%27t%20be%20const%20lint%3F/near/215751047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Restioson <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Reason.20a.20fn.20can&#x27;t.20be.20const.20lint.3F.html#215751047">(Nov 05 2020 at 18:02)</a>:</h4>
<p>ah, apologies for not putting it here. i wasnt sure where to put it originally so thanks for moving it!</p>



<a name="215751114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Reason%20a%20fn%20can%27t%20be%20const%20lint%3F/near/215751114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Restioson <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Reason.20a.20fn.20can&#x27;t.20be.20const.20lint.3F.html#215751114">(Nov 05 2020 at 18:03)</a>:</h4>
<p>i suppose aside from the function in the call graph not being able to be const, it could also just be out of your control to make const (out of your crate)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>